export const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return (
    [year, month, day].map(formatNumber).join('-')
  )
}

const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

/*函数防抖*/
export const debounce = (fn: Function, interval: number) => {
  let timer: any = null
  let gapTime = interval || 1000 //间隔时间，如果interval不传，则默认1000ms
  return function () {
    clearTimeout(timer)
    let context = this
    let args = arguments[0] //保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
    timer = setTimeout(function () {
      fn.call(context, args)
    }, gapTime)
  }
}
